package com.colagy.jdbc;

import com.colagy.jdbc.pojo.UserPOJO;

import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;


public class JDBCTest {

    public static void main(String[] args) {

        String host = "127.0.0.1";
        String port = "3306";
        String database = "colagy";

        String username = "root";
        String password = "colagy_MySql_password+-";

        try {
            JDBCUtil.createConnection(host, port, database, username, password);
        } catch (SQLException e) {
            System.err.println("连接创建出错!");
            e.printStackTrace();
        }

        UserPOJO userPOJO = new UserPOJO();

        userPOJO.setName("biden");
        userPOJO.setAge(78);
        userPOJO.setComment("拜登");
//        userPOJO.setName("trump");
//        userPOJO.setAge(66);
//        userPOJO.setComment("川普");

        UserPOJO wherePojo = new UserPOJO();
        wherePojo.setName("biden");

        try {
            wherePojo.setName("trump");
            UserPOJO userRes = JDBCUtil.selectOne(wherePojo, UserPOJO.class);
            System.out.println(userRes);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            Set<UserPOJO> userPOJOSet = (Set<UserPOJO>) JDBCUtil.selectAll(wherePojo, HashSet.class, UserPOJO.class);
            System.out.println(userPOJOSet);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            JDBCUtil.insert(userPOJO);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            JDBCUtil.update(userPOJO, wherePojo);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            JDBCUtil.delete(wherePojo);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            UserPOJO userPOJO2 = JDBCUtil.selectOne("select * from user where 1=1 and name=?", UserPOJO.class,"biden");
            System.out.println(userPOJO2);
            Set<UserPOJO> userPOJOSet = (Set<UserPOJO>) JDBCUtil.selectAll("select * from user where 1=1", HashSet.class, UserPOJO.class);
            System.out.println(userPOJOSet);
        } catch (SQLException e) {
            System.err.println("查询出错!");
            e.printStackTrace();
        }

        try {
            JDBCUtil.insert("insert user set name=?, age=?,comment=?", "biden", 77, "拜登");
        } catch (SQLException e) {
            System.err.println("插入出错!");
            e.printStackTrace();
        }

        try {
            JDBCUtil.update("update user set name=?, age=?,comment=? where name=?", "biden", 78, "拜登", "biden");
        } catch (SQLException e) {
            System.err.println("更新出错!");
            e.printStackTrace();
        }

        try {
            JDBCUtil.insert("delete from user where id=?", 7);
        } catch (SQLException e) {
            System.err.println("删除出错!");
            e.printStackTrace();
        }
    }

}
